package net.qhzw.vip.qiyu.controller;


import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import net.qhzw.vip.qiyu.entity.SysRole;
import net.qhzw.vip.qiyu.model.AddRoleForm;
import net.qhzw.vip.qiyu.model.RoleMenuVo;
import net.qhzw.vip.qiyu.result.JsonResult;
import net.qhzw.vip.qiyu.service.SysRoleService;
import net.qhzw.vip.qiyu.util.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Arrays;
import java.util.Map;

/***
 * 角色管理
 */
@RestController
@RequestMapping("sys/roles")
@Api(tags = "角色管理")
public class SysRoleController {

    @Autowired
    private SysRoleService sysRoleService;


    @GetMapping
    @ApiOperation(value = "条件分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页"),
            @ApiImplicitParam(name = "size", value = "每页显示的大小"),
            @ApiImplicitParam(name = "name", value = "角色名称"),
    })
//    @PreAuthorize("hasAuthority('sys_role_query')")
    public JsonResult findByPage(@ApiIgnore Page<SysRole> page, String name) {
        page.addOrder(OrderItem.desc("last_update_time"));
        Long shopId = SecurityUtils.getLoginUser().getShopId();
        Map<String, Object> pageInfo = sysRoleService.findByPage(page, shopId, name);
        return JsonResult.ok(pageInfo);
    }


    @GetMapping("/{id}")
    @ApiOperation(value = "查询角色下的菜单")
    public JsonResult<RoleMenuVo> getSysRole(@PathVariable Long id) {
        RoleMenuVo roleMenuVo = sysRoleService.getSysRole(id);
        return JsonResult.ok(roleMenuVo);
    }

    @PostMapping
    @ApiOperation(value = "新增角色")
    //@PreAuthorize("hasAuthority('sys_role_create')")
    public JsonResult add(@RequestBody @Validated AddRoleForm roleForm) {
        Long shopId = SecurityUtils.getLoginUser().getShopId();
        boolean save = sysRoleService.addSysRole(shopId, roleForm);
        if (save) {
            return JsonResult.ok();
        }
        return JsonResult.fail("新增成功");
    }


    @PutMapping
    @ApiOperation(value = "修改角色")
    //@PreAuthorize("hasAuthority('sys_role_create')")
    public JsonResult update(@RequestBody @Validated AddRoleForm roleForm) {
        boolean update = sysRoleService.updateRoleById(roleForm);
        if (update) {
            return JsonResult.ok();
        }
        return JsonResult.fail("新增成功");
    }

    @DeleteMapping("{ids}")
    @ApiOperation(value = "删除一个角色数据")
    public JsonResult delete(@PathVariable String ids) {
        boolean b = sysRoleService.removeByIds(Arrays.asList(ids));
        if (b) {
            return JsonResult.ok();
        }
        return JsonResult.fail("删除失败");
    }


}
